<?php
App::uses('Component', 'Controller');
class CartComponent extends Component {
	var $components = array('Session');
	var $controller;
	
	function startup(Controller $controller)
   {
        $this->controller =&$controller;
   }
   
	public function addOrder($productId, $number)
	{
		$cart = $this->getCart();
		$product = $this->controller->Product->query('call product_getproductbyid('.$productId.');')[0];
		
		$this->refreshProductSummary($product, $number);	
		//Add the product
		$cart[] = $product;
		$this->refreshCartSummary($cart);
		
		//Store the cart
		$this->Session->write('cart', $cart);
		
		return $cart;
	}
	
	public function deleteOrder($orderId)
	{
		$cart = $this->getCart();
		
		if(isset($cart[$orderId]))
		{
			unset($cart[$orderId]);
			$this->refreshCartSummary($cart);
		}else{
			return false;
		}
		//Store the cart
		$this->Session->write('cart', $cart);
		
		return $cart;
	}
	
	public function emptyCart()
	{
		$this->Session->delete('cart');
	}
	
	public function updateOrder($orderId, $number)
	{
		$cart = $this->getCart();
		
		if(isset($cart[$orderId]))
		{
			$this->refreshProductSummary($cart[$orderId], $number);
			$this->refreshCartSummary($cart);
		}else{
			return false;
		}
		//Store the cart
		$this->Session->write('cart', $cart);
		
		return $cart;
	}
	
	public function getCart(){
		//Get the Cart
		if($this->Session->check('cart'))
		{
			$cart = $this->Session->read('cart');
		}else{
			$cart = array();
		}
		return $cart;
	}
	
	public function getProducts(){
		$products = array();
		
		$cart = $this->getCart();
		unset($cart['summary']);
		foreach($cart as $currentProductFromCart){
			$product_id = $currentProductFromCart['products']['id'];
			$quantity = $currentProductFromCart['summary']['number'];
			$value = $currentProductFromCart['summary']['totalPrice'];
			
			$products[] = array('product_id'=>$product_id,'quantity'=>$quantity,'value'=>$value);
		}
		return $products;
	}
	
	public function IsEmpty()
	{
		$products = $this->getProducts();
		return (empty($products));
	}
	
	private function refreshCartSummary(&$cart){		
		$numberOfItems = 0;
		$totalValue = 0;
		$totalAssociateValue = 0;
		$totalBond = 0;
		foreach($cart as $order)
		{
			$numberOfItems += (isset($order['summary']['number'])?$order['summary']['number']:0);
			$totalValue += (isset($order['summary']['totalPrice'])?$order['summary']['totalPrice']:0);
			$totalAssociateValue += (isset($order['summary']['totalPriceAssociate'])?$order['summary']['totalPriceAssociate']:0);
			$totalBond += (isset($order['summary']['bond'])?$order['summary']['bond']:0);
		}
		
		$cart['summary'] = array(
				'number' => $numberOfItems,
				'totalPrice' => $totalValue,
				'totalPriceAssociate'=>$totalAssociateValue,
				'bond' =>$totalBond
		);
	}
	
	private function refreshProductSummary(&$product, $number){
		$price = $product['products']['price'];
		$associatePrice = $product['products']['associateprice'];
		$bond = $product['products']['bond'];
		$product['summary'] = array(
					'number'=>$number , 
					'totalPrice'=>($number * $price),
					'totalPriceAssociate'=>$number * $associatePrice,
					'bond' => $number * $bond
		);
	}
}

?>